package com.imohoo.libs.converter;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.imohoo.jni.Main;

import org.apache.commons.codec.binary.Base64;

import java.io.IOException;
import java.nio.charset.Charset;

import okhttp3.ResponseBody;
import retrofit2.Converter;

final class SpJsonResponseBodyConverterAes<T> implements Converter<ResponseBody, T> {
    private final Gson gson;
    private final TypeAdapter<T> adapter;

    SpJsonResponseBodyConverterAes(Gson gson, TypeAdapter<T> adapter) {
        this.gson = gson;
        this.adapter = adapter;
    }

    @Override
    public T convert(ResponseBody value) throws IOException {
        try {
            byte[] result = Main.abc(Base64.decodeBase64(value.string().getBytes(Charset.forName("UTF-8"))), Main.DECRYPT, 1);
            return adapter.fromJson(new String(result));
        } finally {
            value.close();
        }
    }
}
